###Clear R--------------------
rm(list=ls())

###Function to recode variables to range from lowest (0) to highest (1) observation---------------------
zero1 <- function(x, minx=NA, maxx=NA){
  res <- NA
  if(is.na(minx)) res <- (x - min(x,na.rm=T))/(max(x,na.rm=T) -min(x,na.rm=T))
  if(!is.na(minx)) res <- (x - minx)/(maxx -minx)
  res
}

####function to install packages if they don't exist----------------
ipak <- function(pkg){
  new.pkg <- pkg[!(pkg %in% installed.packages()[, "Package"])]
  if (length(new.pkg)) 
    install.packages(new.pkg, dependencies = TRUE)
  sapply(pkg, require, character.only = TRUE)
}

# usage
packages <- c("psych", "ggplot2", "interplot", "MASS", "foreign",  "car", "stringr", "stargazer", "xtable", "moments", "dplyr")
ipak(packages)

#load data
load("Study5_TTIP_Replication.RData")


#drop if PIH is missing
data <- subset(data,!is.na(PID))
data <- data[!duplicated(data$PID),]

#create PID strength measures
data$dempid <- 1-rowMeans(mapply(zero1,with(data,data.frame(huddyid_dem_1,huddyid_dem_2,huddyid_dem_3,huddyid_dem_4,huddyid_dem_5,huddyid_dem_6,huddyid_dem_7,huddyid_dem_8))),na.rm=T)
data$reppid <- 1-rowMeans(mapply(zero1,with(data,data.frame(Q11_1,Q11_2,Q11_3,Q11_4,Q11_5,Q11_6,Q11_7,Q11_8))),na.rm=T)

#create 1 PID dimension
data$partyidentity <- rowMeans(with(data,data.frame(dempid,reppid)),na.rm = T)

### Need for Cognition----------------------
#reversed coded itmes recoded
data$NfC_3_rec<-(6-data$NfC_3)
data$NfC_4_rec<-(6-data$NfC_4)
data$NfC_5_rec<-(6-data$NfC_5)
data$NfC_7_rec<-(6-data$NfC_7)
data$NfC_8_rec<-(6-data$NfC_8)
data$NfC_9_rec<-(6-data$NfC_9)
data$NfC_12_rec<-(6-data$NfC_12)
data$NfC_16_rec<-(6-data$NfC_16)
data$NfC_17_rec<-(6-data$NfC_17)

#create 18-item scale
data$nfc<-rowMeans(data.frame(data$NfC_1,data$NfC_2,data$NfC_3_rec,data$NfC_4_rec,data$NfC_5_rec,data$NfC_6, data$NfC_7_rec, data$NfC_8_rec, data$NfC_9_rec, data$NfC_10, data$NfC_11, data$NfC_12_rec, data$NfC_13, data$NfC_14, data$NfC_15, data$NfC_16_rec, data$NfC_17_rec, data$NfC_18), na.rm=T)
data$nfc<-zero1(data$nfc)

### Dependent variable ------------
data$supportttip <- 1-zero1(rowMeans(data.frame(data$ttip_complex_reps,data$ttip_complex_dems,data$ttip_simple_reps,data$Q15),na.rm=T))
data <- subset(data,is.na(supportttip)==F)

### Cognitive Reflection Test-------------------------------------
data$CRT1 <- ifelse(data$cr1 %in% c("5",".05","0,05","0.05"),1,0)
data$CRT2 <- ifelse(data$cr2 %in% c("5","05","5.00"),1,0)
data$CRT3 <-  ifelse(data$cr3 %in% c("47"),1,0)
data$CRTall <- rowSums(with(data,data.frame(CRT1,CRT2,CRT3)),na.rm=T)
data$CRTall <- zero1(data$CRTall) ##recode to range from 0-1

### Partisanship---------------
data$pid <- NA
data$pid[is.na(data$dempid)==F]='Democrat'
data$pid[is.na(data$reppid)==F]='Republican'
data$pid[is.na(data$reppid) & is.na(data$dempid)]=NA
data$pid <- as.factor(data$pid)

### Latent cognitive resources------------
latent <- ' cogresources  =~ NA*CRT1+CRT2+CRT3+NfC_1 + NfC_2 + NfC_3_rec + NfC_4_rec + NfC_5_rec + NfC_6+ NfC_7_rec+NfC_8_rec+NfC_9_rec+NfC_10+NfC_11+NfC_12_rec+NfC_13+NfC_14+NfC_15+NfC_16_rec+NfC_17_rec+NfC_18
# fix variance of latent variable
cogresources ~~ 1*cogresources'

fit <- cfa(latent, ordered=c("CRT1", "CRT2", "CRT3", "NFC_1", "NFC_2", "NFC_3_rec", "NFC_4_rec", "NFC_5_rec", "NfC_6", "NFC_7_rec", "NfC_8_rec", "NfC_9_rec", "NFC_10", "NFC_11", "NFC_12_rec", "NFC_13", "NfC_14", "NfC_15", "NfC_16_rec", "NfC_17_rec", "NfC_18"), data=data)
#fit model
data$cogresources<-(lavPredict(fit, type = "lv", method="EBM",  label = TRUE, optim.method = "nlminb"))
data$cogresources<-zero1(data$cogresources)

### Treatment conditions --------------------
data$demrepcondition <- as.factor(car::recode(as.numeric(as.factor(data$DO.BL.CueExperiment)),"1='Democrats Support';2='Democrats Support';3='Republicans Support';4='Republicans Support';else=NA"))
data$simplecomplex <- as.factor(car::recode(as.numeric(as.factor(data$DO.BL.CueExperiment)),"1='Simple';2='Complex';3='Complex';4='Simple';else=NA"))

data$ownpartysupport <- 0
data$ownpartysupport[as.numeric(data$demrepcondition)==1 & data$pid=='Democrat']=1
data$ownpartysupport[as.numeric(data$demrepcondition)==2 & data$pid=='Republican']=1
data$ownpartysupport <- as.factor(data$ownpartysupport)


### Covariates ---------------------------
#gender: male==0, female==1
data$female[data$Q110==1]<-0
data$female[data$Q110==2]<-1
#Age
data$age<-(2016-as.numeric(data$Q116, na.rm=T))

#Race
data$non_white<-ifelse(data$race==1,0,1)

#Education
data$education<-data$hs
data$education<-car::recode(data$education, "2=1; 3=2; 4=2; 5=3; 6=3; 7=3")

#party
data$Republican_dummy<-0
data$Republican_dummy[data$pid1==1]=1
data$Republican_dummy[data$pid4==1]=1


###Save data----------
save(data, file="Study5_TTIP_data.RData")
